<?php

namespace App\Admin\Actions\Audit;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\UserDmLogic;
use App\Models\AuditModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;
use Illuminate\Http\Request;

class AuditRealNameAuthAction extends RowAction
{
    public $name = '审核';

    /**
     * handle
     * @param AuditModel $model
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function handle(AuditModel $model, Request $request): Response
    {
        $model->status = $request->status;
        $model->operate_id = Admin::user()->id;
        if ($model->status === 2) {
            $model->fail_reason = $request->reason;
            UserDmLogic::updateByWhere(['authStatus' => 3], 'userId', $model->user_id);
            return $this->response()->success('操作成功！')->refresh();
        }
        $model->ok_reason = $request->reason;
        $model->save();
        UserDmLogic::updateByWhere(array_merge($model->content, ['authStatus' => 2]), 'userId', $model->user_id);
        mq('cacheUser', (string)$model->user_id);
        OperateLogLogic::add(Admin::user()->user_id, 'admin.realNameAuth.audit');
        return $this->response()->success('操作成功！')->refresh();
    }

    public function form(): void
    {
        $this->select('status', '审核')->options([1 => '同意', 2 => '拒绝'])->default(2)->required();
        $this->text('reason', '原因')->rules('nullable|string|max:255');
    }
}
